package org.magento.pages;

import org.magento.data.CustomerData;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

/*  This class presents  checkout page one 
 * /checkout/onepage/ 
 * On this page user can login, register new user or check out as guest
 * Also all shipping end 
 */

public class ShopingCartStepOne extends Page {
	
	//Locators: 
	@FindBy (id = "login-email")
	public WebElement userLoginEmailTextField;
	
	@FindBy (id = "login-password")
	public WebElement userLoginPasswordTextField;
	
	@FindBy (xpath = "/descendant::div[@class='buttons-set']/button[@type='submit']")
	public WebElement loginSubmitButton;
	
	@FindBy(id = "billing:company")
	public WebElement companyTextField;
	
	@FindBy(id = "billing:street1")
	public WebElement addressTextField;
	
	@FindBy (id = "billing:city")
	public WebElement cityTextField;
	
	@FindBy (id = "billing:postcode")
	public WebElement poastalCodeTextField;
	
	@FindBy (id = "billing:region_id")
	public WebElement stateProvinceList;
		
	@FindBy (id = "billing:country_id")
	public WebElement countyList;
	
	@FindBy (id = "billing:telephone")
	public WebElement telephoneTextField;
	
	@FindBy (id = "billing:fax")
	public WebElement faxTextField;
	
	@FindBy (id = "billing:use_for_shipping_yes")
	public WebElement shipToThisAddressRadioButton;
	
	@FindBy (xpath = "/descendant::div[@id='billing-buttons-container']/button")
	public WebElement  billingContinueButton;
	
	@FindBy (xpath = "/descendant::div[@id = 'shipping-method-buttons-container']/button")
	public WebElement shippingContinueButton;
	
	@FindBy (id = "p_method_checkmo")
	public WebElement paymentMethodMoneyorder;
	
	@FindBy (id ="p_method_ccsave")
	public WebElement paymentMethodCreditCart;
	
	@FindBy (id = "ccsave_cc_owner")
	public WebElement creditCardNameHolder;
	
	@FindBy (id = "ccsave_cc_type")
	public WebElement creditCardType;
	
	@FindBy (id = "ccsave_cc_number")
	public WebElement creditCardNumber;
	
	@FindBy (id = "ccsave_expiration")
	public WebElement creditCardExpirationMonth;
	
	@FindBy (id = "ccsave_expiration_yr")
	public WebElement creditCardExpirationYear;
		
	@FindBy (id = "ccsave_cc_cid")
	public WebElement creditCardCvv;
	
	@FindBy (xpath = "/descendant::div[@id = 'payment-buttons-container']/button")
	public WebElement paymentContinueButton;
	
	@FindBy (xpath = "/descendant::div[@id = 'review-buttons-container']/button")
	public WebElement placeOrderButton;


	
	//Actions:
	
	//Manage logging page
	public void  manageLoginForm(CustomerData customer) {
		if (isElementPresent(userLoginEmailTextField)){
			loginAs(customer);
			} else {
			fillBillingForm(customer);
		}
	}
	
	//Login as a customer 
	public ShopingCartStepOne loginAs(CustomerData customer) {
		type(userLoginEmailTextField, customer.email);
		type(userLoginPasswordTextField, customer.password);
		loginSubmitButton.click();
		return PageFactory.initElements(driver, ShopingCartStepOne.class);
	}
	
	// Filling  in a Billing form. 
	public ShopingCartStepOne fillBillingForm(CustomerData customer) {
		type(companyTextField, customer.company);
		type(addressTextField, customer.address);
		type(cityTextField, customer.city);
		select(countyList, customer.country);
		type(poastalCodeTextField, customer.postcode);
		type(telephoneTextField, customer.telephone);
		type(faxTextField, customer.telephone);
		shipToThisAddressRadioButton.click();
		billingContinueButton.click();
		return PageFactory.initElements(driver, ShopingCartStepOne.class);	
	}
	
	//Click on continue button at a billing form 
	public ShopingCartStepOne saveBillingForm () {
		shippingContinueButton.click();
		return PageFactory.initElements(driver, ShopingCartStepOne.class);	
	}
	
	//Fill in  billing form  if customer is logged for a first time or click continue in other case. 
	public void  manageBilling(CustomerData customer) {
		if (billingInfoPresents()){
			saveBillingForm();
			}  else { fillBillingForm(customer);
		}
	}
	
	//Click continue on a Shipping method step
	public ShopingCartStepOne manageShippingMethodForm() {
		shippingContinueButton.click();
		return PageFactory.initElements(driver, ShopingCartStepOne.class);
	}
	
	
	//Manage payment form by choosing Money order payment type and click Continue
	public ShopingCartStepOne managePaymentMethodForm() throws Exception {
		waitForFiveSeconds();
		paymentMethodMoneyorder.click();
		paymentContinueButton.click();
		return PageFactory.initElements(driver, ShopingCartStepOne.class);
	}
	
	// Chose payment wia credit card and fill in all necessary fields
	public ShopingCartStepOne payWithCreditCard (CustomerData customer) throws Exception { 
		paymentMethodCreditCart.click();
	    type(creditCardNameHolder, customer.creditCardName);
	    select(creditCardType, customer.creditCardType);
	    type(creditCardNumber, customer.creditCardNumber);
		select(creditCardExpirationMonth, customer.creditCardMonth);
		select(creditCardExpirationYear, customer.creditCardYear);
		type(creditCardCvv, customer.creditCardCvv);
		waitForFiveSeconds();
		paymentContinueButton.click();		
		return PageFactory.initElements(driver, ShopingCartStepOne.class);
	}

	
	//Place an Order at Review Order step 
	public ShopingCartStepOne manageReviewOrderForm() {
		placeOrderButton.click();
		return PageFactory.initElements(driver, ShopingCartStepOne.class);
	}

	
	//Assertions:  
	
	//Verification billing fill form. Is user logged for the first time?  
	public boolean  billingInfoPresents() {
		return driver.findElements(By.id("billing:company")).size()!=0; 
	}

	//Verify was product purchased? 
	public boolean isOrderRerformed() {
		return driver.findElement(By.cssSelector("BODY")).getText().contains("Your order has been received.");
	}
	
	//Auto-generated
	public ShopingCartStepOne(WebDriver driver) {
		super(driver);
	}

	@Override
	public void open() {
	}
}
